package com.example.yls.demoa;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private long lastPressTime = 0;
    private final String TAG = "MainActivity";
    private ListView nameListView;
    private String[] nameArr = {"李白","猴子","赵云","韩信","廉颇",
            "张飞","关羽","刘备","周瑜","小乔","大乔","曹操","哪咤",
            "公孙离","貂蝉","吕布","昭君","诸葛亮","司马懿","元歌"};

    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化学生列表
        initStuList();

        nameListView = findViewById(R.id.name_listview);

        //自定义适配器
        adapter = new MyAdapter(MainActivity.this, studentArrayList);

        nameListView.setAdapter(adapter);

    }
//隐藏的name,age在设置Student的 public Student才显示出来
    private void initStuList() {
        Student s1 = new Student(R.drawable.dog1, "张三", 18);
        Student s2 = new Student(R.drawable.dog2, "张四", 19);
        Student s3 = new Student(R.drawable.dog3, "张五", 20);
        Student s4 = new Student(R.drawable.dog4, "张六", 21);
        Student s5 = new Student(R.drawable.dog5, "张七", 22);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
    }

    //再按一次退出的代码
    @Override
    public void onBackPressed() {
        long currentPressTime = System.currentTimeMillis();

        if(currentPressTime - lastPressTime > 2000){
            lastPressTime = currentPressTime;
            Toast.makeText(MainActivity.this,
                    "再按一次退出", Toast.LENGTH_SHORT).show();
        }else {
            super.onBackPressed();
        }
    }


}
